package com.keepers.repair.fragment

import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.v4.app.Fragment
import android.support.v4.view.GravityCompat
import android.support.v4.widget.DrawerLayout
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.keepers.repair.R
import com.keepers.repair.utils.getUser
import org.jetbrains.anko.find

class MainIndexFragment : Fragment() {

    private lateinit var drawerLayout: DrawerLayout
    private lateinit var navigationView: NavigationView
    private lateinit var navHeader: View
    private lateinit var navName: TextView
    private lateinit var navIcon: ImageView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_main_index, container, false)

        this.drawerLayout = view.find(R.id.main_drawer_layout)
        this.navigationView = view.find(R.id.main_navigation_view)
        this.navHeader = navigationView.getHeaderView(0)
        this.navName = navHeader.find(R.id.navigation_header_username)
        this.navIcon = navHeader.find(R.id.navigation_header_icon)

        // Config navigation view
        this.navigationView.setNavigationItemSelectedListener { item ->
            when (item.itemId) {

//                R.id.main_nav_menu_item_group ->
//                    (activity as MainActivity).startFragment(MainGroupFragment())
//                R.id.main_nav_menu_item_task ->
//                    (activity as MainActivity).startFragment(MainTaskFragment())
//                R.id.main_nav_menu_item_repair ->
//                    (activity as MainActivity).startFragment(MainRepairFragment())
//                R.id.main_nav_menu_item_notify ->
//                    (activity as MainActivity).startFragment(MainNotificationFragment())

//                R.id.main_nav_menu_item_chat -> startActivity<ChatActivity>()
//                R.id.main_nav_menu_item_history -> startActivity<ChatActivity>("history" to true)
//
//                R.id.main_nav_menu_item_settings -> startActivity<SettingsActivity>()
//                R.id.main_nav_menu_item_exit -> {
//                    alert("确定退出？") {
//                        yesButton { System.exit(0) }
//                        cancelButton {}
//                    }.show()
//                }
            }
            this.drawerLayout.closeDrawer(GravityCompat.START)
            true
        }
        return view
    }
//
//    override fun onResume() {
//        super.onResume()
//
////        val activity = this.activity as MainActivity
//        val toggle = ActionBarDrawerToggle(
//            activity, drawerLayout, activity.toolbar,
//            R.string.open, R.string.close
//        )
//
//        toggle.isDrawerIndicatorEnabled = true
//        toggle.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp)
//        toggle.syncState()
//        drawerLayout.addDrawerListener(toggle)
//
//        activity.toggle = toggle
//
//        refreshUI()
//    }

    private fun refreshUI() {
        val user = getUser(context!!)
        this.navName.text = user?.UserName
        this.navIcon.setOnClickListener {
            getUser(context!!)?.apply(System.out::println)
        }
    }
}
